package com.lkd.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lkd.dto.NodeQueryDTO;
import com.lkd.entity.Node;
import com.lkd.mapper.BusinessTypeMapper;
import com.lkd.mapper.NodeMapper;
import com.lkd.mapper.RegionMapper;
import com.lkd.mapper.UserMapper;
import com.lkd.report.CollectNodeList;
import com.lkd.report.NodeCollect;
import com.lkd.report.NodeCount;
import com.lkd.result.TableInfo;
import com.lkd.service.NodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Z.lx
 * @Date: 2025/8/17 14:07
 * @Description:
 **/
@Service
public class NodeServiceImpl extends ServiceImpl<NodeMapper, Node> implements NodeService {
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private UserMapper userMapper;

    /**
     * 查询点位管理列表
     *
     * @param nodeQueryDTO 点位管理
     * @return 点位管理
     */
    public TableInfo<Node> selectNodeList(NodeQueryDTO nodeQueryDTO)
    {
        PageHelper.startPage(nodeQueryDTO.getPageIndex(),nodeQueryDTO.getPageSize());
        List<Node> nodeList =nodeMapper.selectNodeVoList(nodeQueryDTO);
        Page< Node> page = (Page< Node>) nodeList;
        return new TableInfo<>(nodeQueryDTO.getPageIndex(),nodeQueryDTO.getPageSize(),page.getPageNum(),page.getTotal(),nodeList);
    }

    /**
     * 新增点位管理
     *
     * @param node 点位管理
     * @return 结果
     */
    public boolean add(Node node) {
        node.setCreateTime(LocalDateTime.now());
        return nodeMapper.insertNode(node);
    }

    /**
     * 修改点位管理
     *
     * @param node 点位管理
     * @return 结果
     */
    public boolean update(Node node) {
        node.setUpdateTime(LocalDateTime.now());
        return nodeMapper.update(node,null)>0;
    }

    @Override
    public boolean delete(String nodeId) {
        return nodeMapper.deleteById(nodeId)>0;
    }

    public Node getByNodeId(String nodeId) {
        return nodeMapper.getByNodeId(nodeId);
    }

    @Override
    public List<NodeCount> nodeCollect() {
        List<NodeCount> nodeCountList = new ArrayList<>();
        List<String> nameList = nodeMapper.nameList();
        //把name封装到你的CountList中
        for (String name : nameList) {
            NodeCount nodeCount = new NodeCount();
            nodeCount.setName(name);
            nodeCount.setValue(nodeMapper.getNodeCountByName(name));
            nodeCountList.add(nodeCount);
        }
        return nodeCountList;
    }

    @Override
    public Integer nodeCount() {
        return userMapper.nodeCount();
    }
}
